const express = require('express');
const router = express.Router();
// const module = require('module')

const multer = require('multer')
const upload = multer({ dest: 'uploads/' });

const jwt = require('jsonwebtoken');
const expressJWT = require('express-jwt');


// 加载内置模块
const path = require('path');

// 加载自定义模块
const db = require(path.join(__dirname, '../db.js'))

// 添加英雄的接口
router.post('/my/addHero', upload.single('avatar_url'), (req, res) => {
    let sql = 'insert into heroList set ?';
    // req.file.filename是上传之后的文件名
    req.body.avatar_url = req.file.filename;
    
    db(sql, req.body, (err, result) => {
        if (err || result.affectedRows < 1) {
            res.json({
                status: 1,
                message: '添加失败'
            })
        } else {
            res.json({
                status: 0,
                message: '添加成功'
            })
        }
    })
})



// 获取英雄数据
router.get('/my/heroeslist', (req, res) => {
    // 设置请求参数并设置默认值
    let pagenum = req.query.pagenum || 1;
    let pagesize = req.query.pagesize || 2;
    let keywords = req.query.keywords || '';
    let m = '';
    if (keywords) {
        m = ` where heroName like '%${keywords}%' or heroNickName like '%${keywords}%' `
    }
    let sql = `select * from heroList ${m} limit ${(pagenum - 1) * pagesize},${pagesize}`
    db('select count(*) cc from heroList' + m, null, (err, result) => {
        if (err) return console.log(err);
        var total = Math.ceil(result[0].cc / pagesize)


        db(sql, null, (e, r) => {
            if (e) return console.log(e);
            res.json({
                status: 0,
                message: '获取数据列表成功',
                data: r,
                total: total
            })
        })
    })
})


// 删除英雄的接口
router.get('/my/deletehero', (req, res) => {
    let id = req.query.id;
    if (!id || isNaN(id)) {

        return res.json({
            status: 1,
            message: '未找到数字格式的id'
        })
    }
    // 执行sql语句
    db('delete from heroList where id=?', id, (err, result) => {
        if (err) {
            res.json({
                status: 1,
                message: '删除英雄失败'
            })
        } else {
            res.json({
                status: 0,
                message: '删除英雄成功'
            })
        }
    })

})



// 根据id获取一个英雄的详细信息
router.get('/my/detail/:id', (req, res) => {
    var id = req.params.id;
    if (!id || isNaN(id)) {
        return res.json({ status: 1, message: '缺少id' })
    }
    db('select * from heroList where id=?', id, (err, result) => {
        if (err) {
            res.json({
                status: 1,
                message: '获取英雄数据失败'
            })
        } else {
            res.json({
                status: 0,
                message: '获取英雄简介成功',
                data: result[0]
            })
        }
    })
})

// 根据id更新英雄
router.post('/my/updatehero', upload.single('avatar_url'), (req, res) => {
    let values = {
        heroName: req.body.heroName,
        heroNickName: req.body.heroNickName,
        skillName: req.body.skillName
    }
    if (req.file) {
        values.avatar_url = req.file.filename
    }
    db('update heroList set ? where id=?', [values, req.body.id], (err, result) => {
        if (err || result.affectedRows < 1) {
            res.json({ status: 1, message: '修改失败' })
        } else {
            res.json({ status: 0, message: '修改成功' })
        }
    })
})




// 使用 module.exports 向外共享路由对象
module.exports = router;